//2 words per sec
var abs_h;
var amount_time;
var amount_word=500;
var asdfg;
var count_coma_fulstop;
var count_hyp;
var curr_path;
var cant_read=["HTML","HEAD","SCRIPT","STYLE","META","LINK","BODY"];
var elegible;
var elegible_main=null;
var element; 
var flag;
var flag_element=0;
var handle;
var mut=0;
var not_count_hyp;
var prev_time;
var prev_tree;
var show="";
var temp_param;
var zxcvb;
$(window).focus(function(){if(flag_element==0){prev_tree=document;make_list_1(document.body);flag_element=1;}console.debug("calling abc() focus");if(mut==0)abc();console.debug("gained focus")});
$(window).blur(function(){after_abc();console.debug("lost focus")});
$(window).scroll(function()
{
	clearTimeout(handle);
	handle=setTimeout("after_abc();console.debug('calling abc() due to scroll');if(mut==0)abc();",1000);
	//handle=setTimeout("console.debug('scrolled')",400);
});
window.onbeforeunload=function()
{
	after_abc();
	makePost();
}
window.onload=initiate;
function update_list()
{
console.debug("here");
console.debug(prev_tree);
if($(prev_tree.body).html()!=$(document.body).html())
	console.debug($(prev_tree).children());
	/*if(curr_path!=$('body').html())
	{
		console.debug("DOM changed");
		make_list();
		console.debug("calling abc()from upd list");
		if(mut==0)
		abc();
	}*/
	setTimeout("update_list();",112000);
}
function makePost()
{
	var d1=new Date();
	d1=d1.getTime();
	amount_time=d1-amount_time;
	amount_time=amount_time/1000;
	if(amount_time*2<amount_word)
		amount_word=amount_time*2;
	//console.debug("here");
	var ee;
	var post=new Array();
	var check=0;
	var len=elegible_main.length;
	elegible_main=elegible_main.sort(sort_list1);
	console.debug(elegible_main);
	ee=0;
	var post_temp;
	while(check<amount_word && ee<len)
	{
		var c=0;
		var post_txt="";
		post_temp=elegible_main[ee].txt.split(" ");
		var word_read;
		if(elegible_main[ee].time_spent*2<elegible_main[ee].len)
			word_read=elegible_main[ee].time_spent*2;
		else	word_read=elegible_main[ee].len;
		for(var i=0;(i<post_temp.length)&&(c<word_read);i++)
			if(post_temp[i]!="")
			{
				post_txt=post_txt+post_temp[i]+" ";
				c++;
			}
		check=check+c;
		var temp_main=new Object();
		temp_main.txt=post_txt;
		temp_main.ht=$(elegible_main[ee].ele).offset().top;
		post.push(temp_main);
		ee++;
	}
	post=post.sort(sort_list2);
	post_txt="";
	for(var i=0;i<post.length;i++)
		post_txt=post_txt+post[i].txt+" ";
	var abc=new Object();
	abc.pos=post_txt;
	abc.hyp=$(location).attr('href');
	var port = chrome.extension.connect({name: "knockknock"});
	port.postMessage(abc);
	//console.debug(abc.hyp);
	//console.debug(abc.pos);
	//console.debug(post_txt);
}
function sort_list1(a,b)
{
	return (a.param1-b.param1);
}
function sort_list2(a,b)
{
	return (a.ht-b.ht);
}
function after_abc()
{
	var d1=new Date();
	var d=d1.getTime();
	d=d-prev_time;
	prev_time=d1.getTime();
	if(elegible_main==null)
		elegible_main=new Array();
	var ee;
	var len=elegible_main.length;
	var matched=0;
	for(var j=0;j<len;j++)
	{
		for(ee=0;ee<elegible.length;ee++)
		{
			if(elegible_main[j].ele==elegible[ee].ele)
			{
				matched++;
				elegible_main[j].time_spent=elegible_main[j].time_spent+d/1000;
				elegible_main[j].param1=elegible_main[j].param1+(d/1000)*elegible[ee].param;
				elegible.splice(ee,1);
			}
		}
		if(matched==5)
			break;
	}
	for(ee=0;ee<elegible.length;ee++)
	{
		var temp=elegible[ee];
		temp.time_spent=d/1000;
		temp.param1=(d/1000)*elegible[ee].param;
		elegible_main.push(temp);
	}
	console.debug(elegible_main);

}
function abc()
{
	mut=1;
	var ngo=new Date();
	ngo=ngo.getTime();
	asdfg=0;
	var d=new Date();
	var param_w=0.4;	//focus distance as percentage
	var param_h=0.2;
	var abs_w=window.innerWidth;
	abs_h=window.innerHeight;
	var abs_area=abs_w*abs_h;
	var rel_h=param_h*abs_h;
	var rel_w=param_w*abs_w;
	var amount_scroll= $(window).scrollTop();
	var need=parseInt(amount_scroll/100);
	var t=1;
	var constraint_h=0;
	var constraint_w=350;
	elegible=null;
	var temp_chk_param=amount_scroll+rel_h;
	temp_param=1000000000;	//was 100 prev
	flag=0;
	count_coma_fulstop=0;
	count_hyp=0;
	not_count_hyp=0;
	for(var ee=0;ee<=parseInt(abs_h/100);ee++)
		work(element[need+ee],constraint_h,constraint_w,amount_scroll,abs_h,abs_w,abs_area,flag,temp_chk_param,rel_w);
	if(elegible!=null)
	{
		console.debug(elegible);
		//show1();
	}
	else	console.debug("change this page its useless");
	var k=new Date();
	show="";
	console.debug("abc() finsihed");
	var bgo=new Date();
	bgo=bgo.getTime();
	console.debug(bgo-ngo);
	mut=0;
}

function show1()
	{
	if(asdfg<5)
		if(elegible[asdfg]==null||elegible[asdfg]==undefined)
			{}
		else
			{
			if(asdfg>0)
				$(elegible[zxcvb].ele).css({'background-color' : 'white', 'font-weight' : 'normal'});
			$(elegible[asdfg].ele).css({'background-color' : 'IndianRed ', 'font-weight' : 'bolder'});
			zxcvb=asdfg;
			}
	if(asdfg==5)
		{
		$(elegible[zxcvb].ele).css({'background-color' : 'white', 'font-weight' : 'normal'});
		return;
		}
	asdfg++;
	setTimeout("show1()",500);
	}
function initiate()
	{
	console.debug("loaded");
	if(document.hasFocus()==true)
		{
		prev_tree=console.debug($(document).clone(true));
		make_list_1(prev_tree);
		flag_element=1;
		console.debug(prev_tree);
		amount_time=new Date();
		amount_time=amount_time.getTime();
		setTimeout("console.debug('calling abc() from initiate');if(mut==0)abc();",100);
		setTimeout("update_list();",6000);
		}
	}
function make_list(ele)
{

}
function make_list_1(ele)
{
	
}
function work(ele,constraint_h,constraint_w,amount_scroll,abs_h,abs_w,abs_area,flag,temp_chk_param,rel_w)
{
	//console.debug(ele);
	for(var j=0;j<ele.length;j++)
	{
		if($(ele[j]).width()>constraint_w && $(ele[j]).height()>constraint_h && $(ele[j]).offset().top<($(window).scrollTop()+abs_h))
		{
			var temp_chk=$(ele[j]).height()+($(ele[j]).offset()).top;
			
			if($(window).scrollTop()<temp_chk/*&&temp_chk<(abs_h+amount_scroll)*/)
			{
				if($(ele[j]).css("display")=="none"||$(ele[j]).css("visibility")=="hidden")
					continue;
				var txt=ele[j].innerText;
				var txt_no=no_words(txt.trim());
				
				if(txt_no==0)
				    continue;
				
				var hyps=$(ele[j]).find('a');
				var hyp_data="";
				
				for(var i=0;i<hyps.length;i++)
					hyp_data=hyp_data+$(hyps[i]).text()+" ";
				
				//console.debug("hypLink Data = "+hyp_data);
				
				var hyp_data_no=no_words(hyp_data);
				//console.debug("no of words = "+hyp_data_no+" text no = "+txt_no);
				var hyp_wt=hyp_data_no/txt_no;
				if((txt_no-hyp_data_no)<10)
					continue;
				//console.debug("hyp wt = "+hyp_wt);
				
				if(hyp_wt>.8)
				    hyp_wt=0;
				
				else if(hyp_wt>.7)
				    hyp_wt=50;
				
				else if(hyp_wt>.5)
				    hyp_wt=150;
				
				else if(hyp_wt>.2)
				    hyp_wt=290;
				
				else 
				    hyp_wt=400;
				
				////console.debug(hyp_wt);
				var dist=Math.sqrt(Math.pow(temp_chk_param-($(ele[j]).offset()).top,2)+Math.pow(rel_w-($(ele[j]).offset()).left,2));
				var widt=$(ele[j]).width();
				widt=widt-350;
				widt=810*Math.sin((Math.PI/180)*(widt/9));
				var extra_length_b=0;
				var extra_length_a=0;
				if($(window).scrollTop()>$(ele[j]).offset)
					extra_length_a=$(window).scrollTop()-$(ele[j]).offset;
				if(temp_chk>($(window).scrollTop()+abs_h))
					extra_length_b=temp_chk-$(window).scrollTop()-abs_h;
				var mulwt=extra_length_b+extra_length_a;
				mulwt=mulwt/$(ele[j]).height();
				var param=dist-hyp_wt-widt;
				if(mulwt!=0)
					if(param<0)
						param=param*(1-mulwt);
					else	param=param/(1-mulwt);
				var aaa;
				if(param>-100)
					continue;
				for(var m=0;m<5;m++)
				{
					aaa=1;
					if(elegible==null)
						elegible=new Array();
					if(elegible[m]==undefined||elegible[m]==null)
					{
						for(var ter=0;ter<5;ter++)
							if(elegible[ter]!=undefined)
							{
								if(elegible[ter].ele==ele[j])
								{
									aaa=0;
									break;
								}
								else if($(elegible[ter].ele).parents().index(ele[j])!=-1)
								{
									var tmp_ob=new Object();
									tmp_ob.depth=$(ele[j]).parents().length;
									tmp_ob.hyptxt=hyp_data;
									tmp_ob.txt=txt;
									tmp_ob.len=txt_no;
									tmp_ob.ele=ele[j];
									tmp_ob.param=param;
									tmp_ob.widt=widt;
									tmp_ob.dist=dist;
									tmp_ob.hyp_wt=hyp_wt;
									tmp_ob.ext_len_a=extra_length_a;
									tmp_ob.ext_len_b=extra_length_b;
									elegible[ter]=tmp_ob;
									aaa=0;
								}
								else if($(ele[j]).parents().index(elegible[ter].ele)!=-1)
								{
								  	aaa=0;
								  	break;
								}
							}
						if(aaa==0)
							break;
						elegible[m]=new Object();
						elegible[m].depth=$(ele[j]).parents().length;
						elegible[m].hyptxt=hyp_data;
						elegible[m].txt=txt;
						elegible[m].len=txt_no;
					  	elegible[m].ele=ele[j];
					  	elegible[m].param=param;
						elegible[m].paren=$(ele[j]).parents();
						elegible[m].widt=widt;
					  	elegible[m].dist=dist;
					  	elegible[m].tt=temp_chk;
					  	elegible[m].hyp_wt=hyp_wt;
					  	elegible[m].ext_len_b=extra_length_b;
					  	elegible[m].ext_len_a=extra_length_a;
					  	//console.debug(" j = "+j+" inner HTML = "+$(ele[j]).html());
					  	//console.debug("for m = "+m$(objArr[i])+" param val = "+param);
					  	break;
					}
				if(param<elegible[m].param)
					{
					for(var ter=0;ter<5;ter++)
						if(elegible[ter]!=undefined)
						{
							if(elegible[ter].ele==ele[j])
							{
								aaa=0;
								break;
							}
							else if($(elegible[ter].ele).parents().index(ele[j])!=-1)
							{
								var tmp_ob=new Object();
								tmp_ob.depth=$(ele[j]).parents().length;
								tmp_ob.hyptxt=hyp_data;
								tmp_ob.txt=txt;
								tmp_ob.len=txt_no;
								tmp_ob.ele=ele[j];
								tmp_ob.param=param;
								tmp_ob.widt=widt;
								tmp_ob.dist=dist;
								tmp_ob.hyp_wt=hyp_wt;
								tmp_ob.ext_len_a=extra_length_a;
								tmp_ob.ext_len_b=extra_length_b;
								elegible[ter]=tmp_ob;
								aaa=0;
							}
							else if($(ele[j]).parents().index(elegible[ter].ele)!=-1)
							{
							  	aaa=0;
							  	break;
							}
						}
					if(aaa==0)
						break;
					var tmp_ob=new Object();
					tmp_ob.depth=$(ele[j]).parents().length;
					tmp_ob.hyptxt=hyp_data;
					tmp_ob.txt=txt;
					tmp_ob.len=txt_no;
				  	tmp_ob.ele=ele[j];
				  	tmp_ob.param=param;
				  	tmp_ob.paren=$(ele[j]).parents();
				  	tmp_ob.widt=widt;
				  	tmp_ob.dist=dist;
				  	tmp_ob.hyp_wt=hyp_wt;
				  	tmp_ob.tt=temp_chk;
				  	tmp_ob.ext_len_a=extra_length_a;
				  	tmp_ob.ext_len_b=extra_length_b;
				  	////console.debug("2",elegible);
				  	elegible.splice(m,0,tmp_ob);
				  	elegible.splice(5,1);
				  	////console.debug("3",elegible);
					break;
					}
				}
				flag=1;
			}
		}	
	}
}
function no_words(str)
	{
	str=str.split(" ");
	var c=0;
	for(var i=0;i<str.length;i++)
		if(str[i]!="")
			c++;
	return c;
	}
